package com.dfbz.springboot_project.config;

import com.dfbz.springboot_project.realm.MyRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
//@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringShiroConfig {
    @Bean("realm")
    public MyRealm getRealm(){
        return new MyRealm();
    }
    @Bean("securityManager")
    @ConditionalOnMissingBean
    public DefaultWebSecurityManager getwebSecurityManager(MyRealm realm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(realm);
        return manager;
    }

    /**
     * 配置过滤器链规则
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition getfilterChainDefinition(){
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        //静态资源不需要认证
        chainDefinition.addPathDefinition("/html/images/**","anon");
        chainDefinition.addPathDefinition("/html/js/**","anon");
        chainDefinition.addPathDefinition("/html/styles/**","anon");
        chainDefinition.addPathDefinition("/common/toLogin","anon");
        chainDefinition.addPathDefinition("/common/logout","logout");
        //chainDefinition.addPathDefinition("/**","authc");
        return chainDefinition;
    }
//    @Bean
//    public ShiroFilterFactoryBean getfilterFactoryBean(SecurityManager manager){
//        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
//        factoryBean.setSecurityManager((org.apache.shiro.mgt.SecurityManager) manager);
//        factoryBean.setLoginUrl("/common/login");
//        return factoryBean;
//    }
}
